import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store/index.js'             // vuex 的引 入     
// const home = ()=> import('@/pages/home/Home.vue')  
const home = () => import('@/pages/home/Home.vue')                                           //  首页
const productList = () => import('@/pages/productList/ProductList.vue')                      //  产品列表
const productDetails = () => import('@/pages/productDetails/ProductDetails.vue')             //  产品详情页
const login = () => import('@/pages/login/Login.vue')                                        //  登陆页面
const register = () => import('@/pages/register/Register.vue')                               //  注册页面
const shoppingCart = () => import('@/pages/shoppingCart/ShoppingCart.vue')                   //  购物车页面
const quotation = () => import('@/pages/quotation/Quotation.vue')                            //  报价单页面
const confirmOrder = () => import('@/pages/confirmOrder/ConfirmOrder.vue')                   //  确认订单
const payment = () => import('@/pages/payment/Payment.vue')                                  //  支付
const paymentSuccess = () => import('@/pages/paymentSuccess/paymentSuccess.vue')             //  支付成功页面
const myOrder = () => import('@/pages/myOrder/MyOrder.vue')                                  //  我的订单 页面
const watchQuotation = () => import('@/pages/myOrder/WatchQuotation.vue')                    //  查看报价单
const brandWorld = () => import('@/pages/brandWorld/BrandWorld.vue')                         //  品牌世界
const purchaseCenter = () => import('@/pages/purchaseCenter/PurchaseCenter.vue')             //  求购中心
const purchaseDetails = () => import('@/pages/purchaseDetails/PurchaseDetails.vue')          //  求购中心的查看详情页面
const preferentialMarket = () => import('@/pages/preferentialMarket/PreferentialMarket.vue') //  特惠卖场
const consultingCenter = () => import('@/pages/consultingCenter/ConsultingCenter.vue')       //  咨询中心
const footerDetails = () => import('@/pages/FooterDetails/FooterDetails.vue')                //  底部导航的详情页面
const brandDetails = () => import('@/pages/brandDetails/BrandDetails.vue')                   //  品牌世界的详情页面
const GotoFudi = () => import('@/pages/gotoFudi/GotoFudi.vue')                               //  走进孚地
const FamousMechanics = () => import('@/pages/famousMechanics/FamousMechanics.vue')          //  名品机械
const CustomerService = () => import('@/pages/customerService/CustomerService.vue')          //  客户服务
const MerchantServices = () => import('@/pages/merchantServices/MerchantServices.vue')       //  商户服务
const AccountService = () => import('@/pages/accountService/AccountService.vue')             //  账期服务
const ReturnGoods = () => import('@/pages/myOrder/ReturnGoods.vue')                          //  退换货
const ReturnState = () => import('@/pages/myOrder/ReturnState.vue')                          //  退货四步
const QuickOrder = () => import('@/pages/quickOrder/QuickOrder.vue')                         //  快速下單
const Recharge = () => import('@/pages/payment/Recharge.vue')                                //  支付跳转
const QrCode = () => import('@/pages/payment/QrCode.vue')                                    //  微信支付二維碼
const HomeSeach = () => import('@/pages/homeSeach/HomeSeach.vue')                            //  首页搜索
const ForgetPassword = () => import('@/pages/ForgetPassword/ForgetPassword.vue')             // 忘记密码页面
const ForgetStep1 = () => import('@/pages/ForgetPassword/components/ForgetStep1.vue')       // 忘记密码第一步
const ForgetStep2 = () => import('@/pages/ForgetPassword/components/ForgetStep2.vue')       // 忘记密码第二步
const ForgetStep3 = () => import('@/pages/ForgetPassword/components/ForgetStep3.vue')       // 忘记密码第三步


Vue.use(Router)

const myRoutes = [
  {
    path: '/',
    name: 'home',
    component: home,                 // 首页
  }, {
    path: '/productList',
    name: 'productList',
    component: productList          // 产品列表
  }, {
    path: '/productDetails/:id',
    name: 'productDetails',
    component: productDetails       // 产品详情页
  }, {
    path: '/login',
    name: 'login',
    component: login               // 登陆页面

  }, {
    path: '/register',
    name: 'register',
    component: register             // 注册页面
  }, {
    path: '/shoppingCart',
    name: 'shoppingCart',
    meta: {
      requireAuth: true,      // 添加该字段，表示进入这个路由是需要登录的
    },
    component: shoppingCart         // 购物车页面
  }, {
    path: '/quotation',
    name: 'quotation',
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },
    component: quotation            // 报价单页面
  }, {
    path: '/confirmOrder',
    name: 'confirmOrder',
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },
    component: confirmOrder         // 确认订单
  }, {
    path: '/payment',
    name: 'payment',
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },
    component: payment             // 支付
  }, {
    path: '/paymentSuccess',
    name: 'paymentSuccess',
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },
    component: paymentSuccess      // 支付成功页面
  }, {
    path: '/myOrder',
    name: 'myOrder',
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },
    component: myOrder,             // 我的订单 页面
    children: [
      {  // 个人中心
        path: 'MyCenter',
        name: 'MyCenter',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/MyCenter.vue'),
        // redirect: { name: 'BasicData' },
        // children: [
        //   {
        //     path: 'BasicData',
        //     name: 'BasicData',
        //     meta: {
        //       requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        //     },
        //     component: () => import('@/pages/myOrder/components/mycenterComponent/BasicData.vue'),
        //   },{
        //     path: 'EnterpriseInformation',
        //     name: 'EnterpriseInformation',
        //     meta: {
        //       requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        //     },
        //     component: () => import('@/pages/myOrder/components/mycenterComponent/EnterpriseInformation.vue'),
        //   },{
        //     path: 'AccountSecurity',
        //     name: 'AccountSecurity',
        //     meta: {
        //       requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        //     },
        //     component: () => import('@/pages/myOrder/components/mycenterComponent/AccountSecurity.vue'),
        //   },{
        //     path: 'GeneralTaxpayers',
        //     name: 'GeneralTaxpayers',
        //     meta: {
        //       requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        //     },
        //     component: () => import('@/pages/myOrder/components/mycenterComponent/GeneralTaxpayers.vue'),
        //   }
        // ]
      }, {  // 我的订单
        path: 'FullOrder',
        name: 'FullOrder',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/FullOrder.vue'),
      }, {  // 我的优惠券
        path: 'MyCoupon',
        name: 'MyCoupon',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/MyCoupon.vue'),
      }, {  // 我的报价单
        path: 'MyQuotation',
        name: 'MyQuotation',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/MyQuotation.vue'),
      }, {  // 我的收藏
        path: 'MyCollection',
        name: 'MyCollection',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/MyCollection.vue'),
      }, {  // 我的地址
        path: 'MyAddress',
        name: 'MyAddress',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/MyAddress.vue'),
      }, {  // 我的发票
        path: 'MyInvoice',
        name: 'MyInvoice',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/MyInvoice.vue'),
      }, {  // 我的求购
        path: 'MyWantBuy',
        name: 'MyWantBuy',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/MyWantBuy.vue'),
      }, {  // 售后维权
        path: 'AfterSale',
        name: 'AfterSale',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/AfterSale.vue'),
      }
      , {  // 订单详情
        path: 'OrderDetails',
        name: 'OrderDetails',
        meta: {
          requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
        },
        component: () => import('@/pages/myOrder/components/OrderDetails.vue'),
      }

    ]
  }, {
    path: '/watchQuotation',
    name: 'watchQuotation',
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },
    component: watchQuotation      //  查看报价单
  }, {
    path: '/brandWorld',
    name: 'brandWorld',
    component: brandWorld          //  品牌世界
  }, {
    path: '/purchaseCenter',
    name: 'purchaseCenter',
    component: purchaseCenter          //  求购中心
  }, {
    path: '/purchaseDetails',
    name: 'purchaseDetails',
    meta: {
      requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
    },
    component: purchaseDetails          //  求购中心的查看详情页面
  }, {
    path: '/preferentialMarket',
    name: 'preferentialMarket',
    component: preferentialMarket          //  特惠卖场
  }, {
    path: '/consultingCenter',
    name: 'consultingCenter',
    component: consultingCenter,          //  咨询中心
    children:[
      {
        path: 'webBulletin:id',
        name: 'webBulletin',
        component: ()=> import('@/pages/consultingCenter/components/webBulletin.vue'),               //  新闻列表
      },{
        path: 'ConsultingDetails',
        name: 'ConsultingDetails',
        component: ()=> import('@/pages/consultingCenter/components/ConsultingDetails.vue'),          //  新闻详情
      }
    ]
  }, {
    path: '/footerDetails',
    name: 'footerDetails',
    component: footerDetails,          //  底部导航的详情页面
    // redirect:'/footerDetails/content1',
    children: [
      {
        path: 'content1',
        name: 'content1',
        component: () => import('@/pages/footerDetails/components/Content1.vue')
      }, {
        path: 'content2',
        name: 'content2',
        component: () => import('@/pages/footerDetails/components/Content2.vue')
      }, {
        path: 'content3',
        name: 'content3',
        component: () => import('@/pages/footerDetails/components/Content3.vue')
      }, {
        path: 'content4',
        name: 'content4',
        component: () => import('@/pages/footerDetails/components/Content4.vue')
      }, {
        path: 'content5',
        name: 'content5',
        component: () => import('@/pages/footerDetails/components/Content5.vue')
      }, {
        path: 'content6',
        name: 'content6',
        component: () => import('@/pages/footerDetails/components/Content6.vue')
      }, {
        path: 'content7',
        name: 'content7',
        component: () => import('@/pages/footerDetails/components/Content7.vue')
      }, {
        path: 'content8',
        name: 'content8',
        component: () => import('@/pages/footerDetails/components/Content8.vue')
      }, {
        path: 'content9',
        name: 'content9',
        component: () => import('@/pages/footerDetails/components/Content9.vue')
      }, {
        path: 'content10',
        name: 'content10',
        component: () => import('@/pages/footerDetails/components/Content10.vue')
      }, {
        path: 'content11',
        name: 'content11',
        component: () => import('@/pages/footerDetails/components/Content11.vue')
      }, {
        path: 'content12',
        name: 'content12',
        component: () => import('@/pages/footerDetails/components/Content12.vue')
      }, {
        path: 'content13',
        name: 'content13',
        component: () => import('@/pages/footerDetails/components/Content13.vue')
      }, {
        path: 'content14',
        name: 'content14',
        component: () => import('@/pages/footerDetails/components/Content14.vue')
      }, {
        path: 'content15',
        name: 'content15',
        component: () => import('@/pages/footerDetails/components/Content15.vue')
      }, {
        path: 'content16',
        name: 'content16',
        component: () => import('@/pages/footerDetails/components/Content16.vue')
      }
    ]
  }, {
    path: '/brandDetails/:id',
    name: 'brandDetails',
    component: brandDetails          //  品牌世界的详情页面
  }, {
    path: '/gotoFudi/:id',
    name: 'GotoFudi',
    component: GotoFudi          //       走进孚地
  }, {
    path: '/FamousMechanics',
    name: 'FamousMechanics',
    component: FamousMechanics    // 名品机械
  }, {
    path: '/customerService',
    name: 'CustomerService',
    component: CustomerService  // 客户服务
  }, {
    path: '/merchantServices',
    name: 'MerchantServices',
    component: MerchantServices  // 商户服务
  }, {
    path: '/AccountService',
    name: 'AccountService',
    component: AccountService  //账期服务
  }, {
    path: '/ReturnGoods',     // 退换货
    name: 'ReturnGoods',
    component: ReturnGoods
  }, {
    path: '/ReturnState',     // 退货四步
    name: 'ReturnState',
    component: ReturnState
  }, {
    path: '/QuickOrder',     // 快速下单
    name: 'QuickOrder',
    meta: {
      requireAuth: true,   // 添加该字段，表示进入这个路由是需要登录的
    },
    component: QuickOrder
  }, {
    path: '/Recharge',     // 快速下单
    name: 'Recharge',
    meta: {
      requireAuth: true,   // 添加该字段，表示进入这个路由是需要登录的
    },
    component: Recharge
  }, {
    path: '/QrCode',     // 快速下单
    name: 'QrCode',
    meta: {
      requireAuth: true,   // 添加该字段，表示进入这个路由是需要登录的
    },
    component: QrCode
  }, {
    path: '/homeSeach',     // 快速下单
    name: 'homeSeach',
    component: HomeSeach
  }, {
    path: '/forgetPassword',
    name: 'ForgetPassword',
    component: ForgetPassword,
    redirect: { path: '/forgetPassword/forgetStep1' },
    children: [
      {
        path: 'forgetStep1',
        name: 'forgetStep1',
        component: ForgetStep1
      },
      {
        path: 'forgetStep2',
        name: 'forgetStep2',
        component: ForgetStep2
      }, {
        path: 'forgetStep3',
        name: 'forgetStep3',
        component: ForgetStep3
      }
    ]
  }

]

// 页面刷新时，重新赋值token
if (localStorage.getItem('token')) {
  store.commit("set_token", localStorage.getItem('token'))
}

const router = new Router({
  mode: "history",     // 需要后台php配合
  // base: '/project-name/', // 如果项目根目录不为域名，则添加该
  // mode: "history"  //这种模式下 path的配置覆盖整个全局 配合  暂时不用可以先来注释掉
  // 给个警告，因为这么做以后，你的服务器就不再返回 404 错误页面，
  // 因为对于所有路径都会返回 index.html 文件。
  // 为了避免这种情况，
  // 应该在 Vue 应用里面覆盖所有的路由情况，然后在给出一个 404 页面。
  routes:[
    ...myRoutes,
    { 
      path: '*', 
      name:'NotFoundComponent',
      component: ()=> import('@/pages/NotFoundComponent.vue') 
    }
  ],
  // 在路由 中 滚动行为 每一次打开后混轮从0开始展现
  scrollBehavior(to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
})

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {           // 判断该路由是否需要登录权限
    if (store.state.token) {           // 通过vuex state获取当前的token是否存在
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  else {
    next()
  }
})


export default router
